package com.dl.config.serializable;

import com.fasterxml.jackson.annotation.JsonCreator;
import org.apache.shiro.web.util.SavedRequest;

import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;

/**
 * 维护重定向请求的请求数据，以便在身份验证之后，可以将用户重定向到最初请求的页面
 * 重写此方法是因为shiro提供的父类没有默认的无参构造，在反序列化时会报异常】
 * 因此继承此类，并且使用注解手动标注使用有参构造进行反序列化
 * @author DL
 * @create 2021-06-08 10:29
 */
public class MySavedRequest extends SavedRequest implements Serializable {
    //TODO - complete JavaDoc

    private final String method;
    private final String queryString;
    private final String requestURI;

    /**
     * Constructs a new instance from the given HTTP request.
     *
     * @param request the current request to save.
     */
    //指定一个有参构造函数供反序列化时调用
    @JsonCreator
    public MySavedRequest(HttpServletRequest request) {
        super(request);
        this.method = request.getMethod();
        this.queryString = request.getQueryString();
        this.requestURI = request.getRequestURI();
    }

    public String getMethod() {
        return method;
    }

    public String getQueryString() {
        return queryString;
    }

    public String getRequestURI() {
        return requestURI;
    }

    public String getRequestUrl() {
        StringBuilder requestUrl = new StringBuilder(getRequestURI());
        if (getQueryString() != null) {
            requestUrl.append("?").append(getQueryString());
        }
        return requestUrl.toString();
    }
}
